package com.company.project.common.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import com.company.project.common.config.FileUploadProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.springframework.stereotype.Service;

/**
 * 二维码工具类
 * @author ZYF
 * @date 2021/5/21 16:59
 */
@Slf4j
@Service
public class ZxingUtil {

    @Resource
    private  FileUploadProperties fileUploadProperties;

    /**
     * 生成并下载二维码
     * @param width 二维码宽
     * @param height 二维码高
     * @param format  二维码格式
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public  ResponseEntity<byte[]> getResponseEntity(Object content, int width, int height, String format) throws WriterException, IOException {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content.toString(),
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        //将矩阵转为Image
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, format, out);//将BufferedImage转成out输出流
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(out.toByteArray(),
                headers, HttpStatus.CREATED);
    }
    /**
     * 生成并下载二维码
     * @param code 需要放的内容
     * @param width 二维码宽
     * @param height 二维码高
     * @param format  二维码格式
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public  String createQRImg(String code,int width, int height,String format) throws WriterException, IOException {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        //将矩阵转为Image
        StringBuilder name=new StringBuilder().append(DateUtils.format(new Date(), DateUtils.DATEPATTERN));
        StringBuilder path =new StringBuilder();
        String newPath = fileUploadProperties.getPath()  +"/qr/";
        path.append(newPath);
        path.append(name);
        path.append(".png");
        File f = new File(path.toString());
        // 创建文件夹
        if (!f.exists()) {
            f.mkdirs();
        }
        Path file = new File(path.toString()).toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        //拼接图片地址
        name.append(".png");
        log.info(name.toString());
        String fileUrl = fileUploadProperties.getUrl()+ "/" +name;
        log.info(fileUrl);
        return	fileUrl;
    }
}
